﻿var link_calljs = function (jo, options) {
    var attrobj = Utils.getAttrObject(jo, "calljs");
    var settings = $.extend({}, options, attrobj);
    var method = settings.method;
    method(jo, settings);
}

//普通连接，跳转前确认
function hyt_link_afterConfirm(jo, options) {
    var settings = $.extend({
        url: jo.attr("href"),
        confirm: jo.attr("confirm")
    }, options);
    mAlertManager.confirm(settings.confirm, function () {
        location.href = settings.url.nocache();
    });
};

//单个查找带回
function hyt_link_selectback(jo, options) {
    mWindowPointer.up().notify(intent.action.callback, options.backdata);
    mWindowPointer.notify(intent.action.closewindow);
}

//列表页查询
function hyt_link_listquery(jo, options) {
    var searchbar = jo.closest(".searchbar");
    var controls = searchbar.find("input,textarea");
    controls.each(function () {
        $(this).prop("lastvalue", $(this).val());
    });
    $(".datagrid").get(0).grid.changePage("first");
    $(".datagrid").get(0).grid.populate();
}

//多个选中项回发
function hyt_link_multichkPostback(jo, options) {
    var settings = $.extend({
        datatype: $(jo).attr("datatype") || "json",
        onsuccess: Utils.eval($(jo).attr("onsuccess") || "hyt_link_multichkPostback_onsuccess_auto"),
        onfail: Utils.eval($(jo).attr("onfail") || "_hyt_ajax_onfail"),
        onexcption: Utils.eval($(jo).attr("onexcption") || "function(){}"),
        ontimeout: Utils.eval($(jo).attr("ontimeout") || "_hyt_ajax_ontimeout"),
        onservererror: Utils.eval($(jo).attr("onservererror") || "_hyt_ajax_onservererror"),
        action: { alert: { message: '操作成功' }, refresh: true, notifyparent: true },
        confirm: '确定进行操作？',
        filter: '.pk'
    }, options);
    var controls = $(settings.filter + ":checked");
    var values = [];
    controls.each(function () {
        values.push({name:"id",value: $(this).val() });
    });
    if (values.length == 0) {
        mAlertManager.error("你没有选择任何项",2000);
        return;
    }
    var send = function () {
        $.ajax({
            url: settings.url,
            data: values,
            type: "POST",
            dataType: settings.datatype,
            success: function (rspdata, state, rspraw) {
                //var allheaer = rspraw.getAllResponseHeaders();
                var contentype = rspraw.getResponseHeader("Content-Type");

                if (contentype.indexOf("application/json") != -1) {
                    if (rspdata.success) {
                        settings.onsuccess(rspdata, rspraw, settings);
                        
                    } else {
                        settings.onfail(rspdata, rspraw);
                    }
                } else if (contentype.indexOf("text/html") != -1) {
                    settings.onsuccess(rspdata, rspraw, settings);
                } else {
                    mAlertManager.error("返回信息不正确");
                }
            },
            error: function (err, errtype) {
                if (errtype == "parsererror") {
                    mAlertManager.error("返回信息不正确");
                } else {
                    settings.onservererror(err, errtype);
                }
            },
            timeout: function () {
                settings.ontimeout();
            }
        });
    };
    if (settings.confirm) {
        mAlertManager.confirm(settings.confirm, function () {
            send();
        });
    } else {
        send();
    }
};

var hyt_link_multichkPostback_onsuccess_auto = function (rspdata, rspraw, settings) {
    var contentype = rspraw.getResponseHeader("Content-Type");
    if (contentype.indexOf("application/json") != -1) {
        if (settings.action.alert) {
            var isobject = typeof (settings.action.alert) == "object"
            mAlertManager.alert(rspdata.message || (isobject && settings.action.alert.message) || "操作成功", (isobject && settings.action.alert.autoclose) || 0);
        }
        if (settings.action.notifyparent) {
            mWindowPointer.up().notify(intent.action.dataischange);
        }
        if (settings.action.closeme) {
            mWindowPointer.notify(intent.action.closewindow);
        }
        if (settings.action.refresh) {
            $(".datagrid").get(0).grid.populate();
        }
    } 
}
